/* *****************************************************************************
 * JFab (http://code.google.com/p/jfab)
 * Copyright (c) 2011-2012 JFab.org
 * Admin jfab@jeffreythompson.net
 *
 * See the file "LICENSE.txt" for information on usage and redistribution of
 * this file, and for a DISCLAIMER OF ALL WARRANTIES.
 * *****************************************************************************/

package org.jfab.core.io;

import java.io.Reader;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Unmarshaller;

import org.jfab.core.model.CGSpec;

/**
 * Provides a reader for code generator XML documents.
 */
public class CGSpecReader
{
    /**
     * Read a code generator XML document.
     * 
     * @param reader Reader.
     * 
     * @return code generator root.
     */
    public CGSpec read(final Reader reader)
    {
        CGSpec answer = null;

        try
        {
            final JAXBContext jaxbContext = JAXBContext.newInstance(Constants.PACKAGE_NAME);
            final Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
            answer = (CGSpec)unmarshaller.unmarshal(reader);
        }
        catch (final JAXBException e)
        {
            throw new IOReaderException(e);
        }

        return answer;
    }
}
